import Notify from '@vant/weapp/notify/notify';
const curd = getApp().curd;
Component({
  properties: {
    list: {
      type: Array,
      value: []
    },
    collection: {
      type: String,
      value: ''
    }
  },
  data: {
    list: []
  },
  methods: {
    // 删除一项待办
    delRecord(e) {
      const id = e.currentTarget.dataset.id
      curd.delete(this.properties.collection, id, () => {
        this.getRecord(this.data.OPENID);
        Notify({
          message: '删除成功✔',
          color: '#fff',
          background: 'rgba(255, 64, 64, 1)',
          duration: 800,
          safeAreaInsetTop: true
        });
      });
    },
    // 点击完成或未完成
    compete(e) {
      const id = e.currentTarget.dataset.id;
      const state = e.currentTarget.dataset.state;
      curd.update(this.properties.collection, id, { state: !state }, () => {
        state ?
          Notify({
            message: 'OK👌',
            color: '#fff',
            background: 'rgba(112, 239, 203, 1)',
            duration: 800,
            safeAreaInsetTop: true
          }) :
          Notify({
            message: 'Nice!🎉',
            color: '#fff',
            background: 'rgba(40, 207, 237, 1)',
            duration: 800,
            safeAreaInsetTop: true
          })
        this.getRecord(this.data.OPENID);
      })
    },
    // 获取待办列表
    getRecord(OPENID) {
      curd.retrieve(this.properties.collection, { _openid: OPENID }, (list) => {
        this.setData({ list });
      });
    },
  },
  lifetimes: {
    attached() {
    }
  }
})



